package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.dto.report.HandlerReportDto;
import com.hbx.dto.report.ReportDto;
import com.hbx.dto.report.SearchReportDto;
import com.hbx.service.ReportService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/report")
@Api(tags = "举报处理器")
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 举报用户资源
     * @param reportDto
     * @param result
     * @return
     */
    @RequestMapping(value = "/addReport",method = RequestMethod.POST)
    @ApiOperation("举报资源")
    @SensitiveFilter
    public Result addReport(@RequestBody @Valid ReportDto reportDto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        reportService.addReport(reportDto, r);
        return r;
    }

    /**
     * 管理员获得举报列表
     * @param dto
     * @return
     */
    @RequestMapping(value = "/reportList",method = RequestMethod.POST)
    @ApiOperation("管理员获得举报列表")
    public Result reportList(@RequestBody SearchReportDto dto){
        Result r = new Result();
        if (ObjectUtils.isEmpty(dto)){
            r.setCode(400).setData("入参错误");
            return r;
        }
        if (dto.getPageNum() == null || dto.getPageNum() < 0){
            r.setCode(400).setData("入参pageNum错误");
            return r;
        }
        if (dto.getPageSize() == null || dto.getPageSize() < 0){
            r.setCode(400).setData("入参pageSize错误");
            return r;
        }
        reportService.reportList(dto, r);
        return r;
    }

    /**
     * 管理员处理举报
     * @param dto
     * @return
     */
    @RequestMapping(value = "/handlerReport",method = RequestMethod.POST)
    @ApiOperation("管理员处理举报")
    @SensitiveFilter
    public Result handlerReport(@RequestBody @Valid HandlerReportDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setData(result.getFieldError().getDefaultMessage());
            return r;
        }
        reportService.handlerReport(dto, r);
        return r;
    }

    /**
     * 获取举报详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/getReportDetail",method = RequestMethod.GET)
    @ApiOperation("获取举报详情")
    public Result getReportDetail(@RequestParam("id") String id){
        Result r = new Result();
        if (!StringUtils.hasLength(id)){
            r.setCode(400).setData("入参错误");
            return r;
        }
        reportService.getReportDetail(id, r);
        return r;
    }

    /**
     * 获得我的举报列表
     * @return
     */
    @RequestMapping(value = "/getMyReport",method = RequestMethod.GET)
    @ApiOperation("获得我的举报列表")
    public Result getMyReport(){
        Result r = new Result();
        reportService.getMyReport(r);
        return r;
    }
}

